<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>增加修改页面</title>
    <link rel="stylesheet" href="layui/layui/css/layui.css" media="all">
    <script type="text/javascript" src="../../res/js/jquery-3.1.1.min.js"></script>
    <script type="text/javascript" src="layui/layui/layui.js"></script>
    <script type="text/javascript" src="../../res/js/tools.js"></script>
    <!--<script type="text/javascript" src="../../res/layui/layui-xtree.js"></script>-->
    <style type="text/css">
        #sportFrom{
            margin: 40px;
        }
        .downpanel .layui-select-title span{
            line-height: 38px;
        }
        .downpanel dl dd:hover{background-color: inherit;}
    </style>
</head>
<body>

<form class="layui-form" action="" lay-filter="example" id="sportFrom">
    <input type="hidden" name="sportId" id="sportId"/>

    <div class="layui-form-item">
        <label class="layui-form-label">项目名</label>
        <div class="layui-input-block">
            <input type="text" id="sportName" name="sportName" lay-verify="required" lay-reqtext="项目名是必填项，岂能为空？" placeholder="请输入项目名" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">选择指标</label>
            <div style="display: inline-block; width: 270px; height: 100%; padding: 10px; overflow: auto;">
                <ul id="updateModuleTree"></ul>
            </div>
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">项目说明</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入说明内容" class="layui-textarea" id="sportExp" name="sportExp"></textarea>
        </div>
    </div>

    <!--<div class="layui-form-item layui-form-text">-->
        <!--<label class="layui-form-label">备注</label>-->
        <!--<div class="layui-input-block">-->
            <!--<textarea placeholder="请输入说明内容" class="layui-textarea" id="remark" name="remark"></textarea>-->
        <!--</div>-->
    <!--</div>-->

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="demo1" lay-filter="demo1">立即提交</button>
        </div>
    </div>
</form>
<script>
    layui.use(['form', 'layedit', 'laydate', 'tree'], function() {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            ,$=layui.jquery
            ,tree=layui.tree;

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        var url = "../../BaseAction/treeNode";
        $.post(url, null, function (data) {
            //开启复选框
          /*  var xtree3 = new layuiXtree({
                elem: '#updateModuleTree'
                ,data: data
                ,showCheckbox: true  //是否显示复选框
                ,id: 'demoId1'
                ,isJump: true //是否允许点击节点时弹出新窗口跳转
                , ckall: true    //启用全选功能，默认值：fals
            });*/
            tree.render({
                elem: '#updateModuleTree'
                ,data: data
                ,showCheckbox: true  //是否显示复选框
                ,id: 'demoId1'
                ,isJump: true //是否允许点击节点时弹出新窗口跳

            });
        }, "json");

        //监听提交
        form.on('submit(demo1)', function(data){
            var id = "";
            $("input[type='checkbox']:checked").each(function(index,items){
                if($(this).val()!= "0" && $(this).val() !="on"){
                    id+=$(this).val()+",";
                }

            });
            data.field.sportId=id;
            //var formData = new FormData(document.getElementById("sportFrom"));
            var formData =  $("#sportFrom").serialize();
                formData += "&itemIds="+id;
            var url = "../../BaseAction/addAndUpdate";
            $.post(url,formData,function(obj){
                console.info(obj.state)
                if(obj.num==1){
                    layer.msg(obj.msg);
                    window.parent.location.reload();
                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                }else{
                    layer.msg(obj.msg);
                }

            },"json")
            /*$.ajax({
                url:url,
                type:"post",
                data:formData,
                processData:false,
                contentType:false,
                dataType:"json",
                success:function(obj){

                },
                error:function(e){
                    layer.msg("操作错误!");
                }
            });*/
            return false;
        });
        var id = getQueryString("id");
        if(id!=null){
            var url = "../../BaseAction/findById";
            var data = {id:id};
            $.post(url,data,function(obj){
                $("#sportId").val(obj.sportId);
                $("#sportName").val(obj.sportName);
                $("#sportExp").val(obj.sportExp);
                $("#remark").val(obj.remark);
                var url = "../../BaseAction/treeNode?sportId="+getQueryString("id");

                var data = {id:id}
                $.post(url, data, function (data) {
                    console.info(data);
                    console.info("=====================");

                    tree.render({
                        elem: '#updateModuleTree'
                        ,data: data
                        ,showCheckbox: true  //是否显示复选框
                        ,id: 'demoId1'
                        ,isJump: true //是否允许点击节点时弹出新窗口跳

                    });

                },'json')
            },'json')
        }
    });
</script>

</body>
</html>